class PortalController < ApplicationController
  def index
  end

  def register
    if (params[:password]!=params[:passwordcon])
      print "Error1";
    
    else
      user = User.new(:name=>params[:name],:surname=>params[:surname],:email=>params[:email],:password=>params[:password],:typee=>params[:type],:status=>"waiting");
      if (params[:type]=="Student") 
        user.group = params[:group]; 
      end
      if (user.save)
        print "Success";
      else
        print "Error3";
      end
    end
redirect_to :action=>"index"; 

  end

  def groups
  @users = User.where(:status=>"waiting");
  @groups = Group.all
  @advisors = User.where(:typee=>"Advisor");
  end

  def savegroup 
    group = Group.new(:name=>params[:name],:faculty=>params[:faculty],:course=>params[:course],:userid=>params[:userid])
    group.save
    redirect_to :action=>"groups"; 
  end

  def saveeditedgroup
    group = Group.find(params[:group]);
    group.name = params[:name];
    group.faculty = params[:faculty];
    group.course = params[:course];
    group.userid = params[:userid];
    
    group.save
    redirect_to :action=>"groups"; 
  end

  def editgroup
    @group = Group.find(params[:id]);
    
    @users = User.where(:status=>"waiting");
    @advisors = User.where(:typee=>"Advisor");
  end


  def deletegroup
    Group.destroy(params[:id]);
    redirect_to :action=>"groups";
  end

  def signin
    #//user = User.find_by_email(params[:email]);
    user = User.authenticate(params[:email],params[:password]);
    
    if (user) 
      session[:userid] = user.id;
      session[:usertype] = user.typee;
      session[:useremail] = user.email;
      
      redirect_to :action=>"mainpage";
	
      print "Success"+user.email;
    else 
      print "Error";
      redirect_to :action=>"index";
    end

    
  end

  def newgroup
    @users = User.where(:status=>"waiting");
    @advisors = User.where(:typee=>"Advisor");

  end

  def logout

    session[:userid] = nil;
      session[:usertype] = nil;
      session[:useremail] = nil;
    redirect_to :action=>"index";
  end

  def mainpage
    @users = User.where(:status=>"waiting");
    @activities = Activity.all
  end

  def profile 
    @users = User.where(:status=>"waiting");
    @user = User.find(params[:id]);
  end

  def myactivities 
    @activities = Activity.where(:author=>session[:useremail]);
    @users = User.where(:status=>"waiting");
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @activities }
    end
  end

  def approve
  	@users = User.where(:status=>"waiting");

  end

  def editprofile
    @users = User.where(:status=>"waiting");
  end

  def approving 
  	@user = User.find(params[:id]);
  	@user.status = "approved";
    @user.save;
  	redirect_to :action=>"approve";
  end

  def registration
  	if (params[:type]=='student')
  		@type = 'Student'
  	  @groups = Group.all
  	elsif (params[:type]=='advisor')
  			@type = 'Advisor'
        
  	else
  		render :file => "#{Rails.root}/public/404.html", :status => :not_found
  	
  	end

  end



end
